# plugins/console_plugin/SConstruct
import os
from SCons.Script import *

# Import variables passed from the main SConstruct
Import('env', 'lib_dir', 'plugin_name') # Import plugin_name if needed

print(f"--- Building Plugin: {plugin_name} ---")

# Plugin source files
plugin_src_files = Glob('*.c')
print(f"  Sources: {[str(s) for s in plugin_src_files]}")

# Ensure the environment includes necessary paths (already done in main SConstruct)
# env.Append(CPPPATH=['../../core', '../../3rd']) # Should not be needed if main env is correct

# Build the shared library (.so)
# Target path is inside the main build/lib directory
plugin_target_path = os.path.join(lib_dir, plugin_name) # e.g., build/lib/console_plugin

# Clone the environment to avoid modifying the one passed in
plugin_env = env.Clone()

# *** Crucial: Link the plugin against libmpack.so ***
# The linker will search in LIBPATH (already set to [build/lib] in env)
plugin_env.Append(LIBS=['mpack'])

plugin_so = plugin_env.SharedLibrary(
    target=plugin_target_path,
    source=plugin_src_files
    # LIBS=['mpack'] # Can also be added here instead of env.Append above
)

print(f"  Target: {plugin_so[0]}")
print(f"--- Finished Plugin: {plugin_name} ---")

# Return the built shared library node so the main SConstruct knows about it
print(f"  Returning plugin node: {plugin_so} (type: {type(plugin_so)})") # Add this
Return(plugin_so)
